﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Newtonsoft.Json;

namespace ConsoleCsharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string file = @"D:\down\test2.txt";
            StreamReader streamReader = new StreamReader(file);
            string txt = streamReader.ReadToEnd();
            streamReader.Close();

            //AlignmentParametersInput item = JsonConvert.DeserializeObject<AlignmentParametersInput>(txt);
            //float x = item.CaliberationDeviation;

            //item.PetDicomOffsetX = (float)Math.PI;
            //item.PetDicomOffsetY = (float) Math.E;


            AlignmentParametersInput item = new AlignmentParametersInput() { CtZEnd = -320.5585f };
            List<Coordinate> ctlist=new List<Coordinate>();
            for (int i = 0; i < 6; i++)
            {
                Coordinate coordinate = new Coordinate() { X = (float)Math.PI * i, Y = (float)Math.PI * i, Z = (float)Math.PI * i };
                ctlist.Add(coordinate);
            }
            List<Coordinate> petlist = new List<Coordinate>();
            for (int i = 0; i < 6; i++)
            {
                Coordinate coordinate = new Coordinate() { X = (float)Math.E * i, Y = (float)Math.E * i, Z = (float)Math.E * i };
                petlist.Add(coordinate);
            }
            item.CtCoordinates = ctlist;
            item.PetCoordinates = petlist;

            string output = JsonConvert.SerializeObject(item, Formatting.Indented);
            StreamWriter streamWriter = new StreamWriter(file);
            streamWriter.Write(output);
            streamWriter.Close();


            string xmlFile = @"D:\down\AlignmentParametersInput.txt";
            CommonXmlSerializer.SerializeToFile(xmlFile, item);
            AlignmentParametersInput item2 = CommonXmlSerializer.DeserializeFromFile<AlignmentParametersInput>(xmlFile);
            float zend = item2.CtZEnd;




            AcParameter acParameter = new AcParameter() { XOffset = -3.3701f, YOffset = 1.5668f, ZOffset = -0.5018f, Qx = -0.0009f, Qy = -0.0016f, Qz = 0.0005f, Qw = 1.0000f };
            DicomParameter dicomParameter = new DicomParameter() { PetDicomOffsetX = 3.2745f, PetDicomOffsetY = -1.3383f, CaliberationDeviation = 3.3450f };
            MechanicsParameter mechanicsParameter = new MechanicsParameter() {XOffSetPetDectector = -3.2745f, YOffSetPetDectector = 1.3383f, ZOffSetPetDectector = -3.3450f, XRorationPetDectector = -0.1255f, YRorationPetDectector = -0.1723f, ZRorationPetDectector = 0.3665f};
            AlignmentParametersOutput alignmentParametersOutput = new AlignmentParametersOutput() {AcParameters = acParameter, DicomParameters = dicomParameter, MechanicsParameters = mechanicsParameter};

            string testOut = @"D:\down\AlignmentParametersOutput.txt";
            CommonXmlSerializer.SerializeToFile(testOut, alignmentParametersOutput);
            //string output2 = JsonConvert.SerializeObject(alignmentParametersOutput, Formatting.Indented);
            //StreamWriter streamWriter2 = new StreamWriter(testOut);
            //streamWriter2.Write(output2);
            //streamWriter2.Close();

            AlignmentParametersOutput item4 = CommonXmlSerializer.DeserializeFromFile<AlignmentParametersOutput>(testOut);
            float sss = item4.AcParameters.Qx;

        }
    }

    public class Coordinate
    {
        public float X { get; set; }
        public float Y { get; set; }
        public float Z { get; set; }
    }

    public class AlignmentParametersInput
    {
        public float CtZEnd { get; set; }
        public List<Coordinate> CtCoordinates { get; set; }
        public List<Coordinate> PetCoordinates { get; set; }
    }

    public class AlignmentParametersOutput
    {
        public AcParameter AcParameters { get; set; }
        public DicomParameter DicomParameters { get; set; }
        public MechanicsParameter MechanicsParameters { get; set; }
    }

    public class AcParameter
    {
        public float XOffset { get; set; }
        public float YOffset { get; set; }
        public float ZOffset { get; set; }

        public float Qx { get; set; }
        public float Qy { get; set; }
        public float Qz { get; set; }
        public float Qw { get; set; }
    }

    public class DicomParameter
    {
        public float PetDicomOffsetX { get; set; }
        public float PetDicomOffsetY { get; set; }
        public float CaliberationDeviation { get; set; }
    }

    public class MechanicsParameter
    {
        public float XOffSetPetDectector { get; set; }
        public float YOffSetPetDectector { get; set; }
        public float ZOffSetPetDectector { get; set; }
        public float XRorationPetDectector { get; set; }
        public float YRorationPetDectector { get; set; }
        public float ZRorationPetDectector { get; set; }
    }

    public class CommonXmlSerializer
    {
        public static void SerializeToFile<T>(string filePath, T instance) where T : class
        {
            using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                var serializer = new XmlSerializer(typeof(T));
                var serializerNameSpace = new XmlSerializerNamespaces();
                serializerNameSpace.Add("", "");
                serializer.Serialize(fs, instance, serializerNameSpace);
                fs.Close();
            }
        }

        public static T DeserializeFromFile<T>(string filePath) where T : class
        {
            var instance = default(T);
            using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                var serializer = new XmlSerializer(typeof(T));
                instance = serializer.Deserialize(fs) as T;
                fs.Close();
            }
            return instance;
        }
    }
}
